/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia;

import java.util.ArrayList;
import java.util.List;

import org.sourceprojects.lycia.annotations.Namespace;
import org.sourceprojects.lycia.annotations.Tag;
import org.sourceprojects.lycia.annotations.XPath;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.sourceprojects.lycia.internal.configuration.NamespaceInnerImpl;
import org.sourceprojects.lycia.internal.configuration.TagInnerImpl;
import org.sourceprojects.lycia.internal.configuration.XPathInnerImpl;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * Abstract implementation of
 * {@link org.sourceprojects.lycia.TagDefinitionParser} with basic handling of
 * some of the needed method structures. If someone want's to implement a tag
 * parser / transformer, he must extends this class instead of implementing the
 * interface directly.
 * 
 * @author noctarius
 * @since 0.0.1
 * 
 * @param <B>
 *            Type of ContextObject to use
 */
public abstract class AbstractTagParser<B> implements TagDefinitionParser<B> {
	private static final Namespace[] ZERO_LENGTH_NAMESPACE_ARRAY = new Namespace[] { new NamespaceInnerImpl(
			null, null) };

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sourceprojects.lycia.TagDefinitionParser#parseElement(org.w3c
	 * .dom.Element, org.sourceprojects.lycia.ParserContext,
	 * org.sourceprojects.lycia.DefinitionBuilder)
	 */
	@Override
	public void parseElement(final Element element,
			final ParserContext<B> parserContext,
			final DefinitionBuilder definitionBuilder)
			throws LyciaParsingException {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sourceprojects.lycia.TagDefinitionParser#parseElement(org.w3c
	 * .dom.Element, java.lang.String, org.sourceprojects.lycia.ParserContext,
	 * org.sourceprojects.lycia.DefinitionBuilder)
	 */
	@Override
	public void parseElement(final Element element, final String localName,
			final ParserContext<B> parserContext,
			final DefinitionBuilder definitionBuilder)
			throws LyciaParsingException {

		parseElement(element, parserContext, definitionBuilder);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.sourceprojects.lycia.TagDefinitionParser#parseElement(org.w3c.dom
	 * .Element, java.lang.String, org.sourceprojects.lycia.QualifiedName,
	 * org.sourceprojects.lycia.ParserContext,
	 * org.sourceprojects.lycia.DefinitionBuilder)
	 */
	@Override
	public void parseElement(final Element element, final String localName,
			final QualifiedName parent, final ParserContext<B> parserContext,
			final DefinitionBuilder definitionBuilder)
			throws LyciaParsingException {

		parseElement(element, localName, parserContext, definitionBuilder);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sourceprojects.lycia.TagDefinitionParser#transformElement(org
	 * .w3c.dom.Element, org.sourceprojects.lycia.ParserContext,
	 * org.sourceprojects.lycia.DefinitionBuilder)
	 */
	@Override
	public Node transformElement(final Element element,
			final ParserContext<B> parserContext,
			final DefinitionBuilder definitionBuilder)
			throws LyciaParsingException {

		return definitionBuilder.getDocument().importNode(element, false);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sourceprojects.lycia.TagDefinitionParser#transformElement(org
	 * .w3c.dom.Element, java.lang.String,
	 * org.sourceprojects.lycia.ParserContext,
	 * org.sourceprojects.lycia.DefinitionBuilder)
	 */
	@Override
	public Node transformElement(final Element element, final String localName,
			final ParserContext<B> parserContext,
			final DefinitionBuilder definitionBuilder)
			throws LyciaParsingException {

		return transformElement(element, parserContext, definitionBuilder);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.sourceprojects.lycia.TagDefinitionParser#transformElement(org.w3c
	 * .dom.Element, java.lang.String, org.sourceprojects.lycia.QualifiedName,
	 * org.sourceprojects.lycia.ParserContext,
	 * org.sourceprojects.lycia.DefinitionBuilder)
	 */
	@Override
	public Node transformElement(final Element element, final String localName,
			final QualifiedName parent, final ParserContext<B> parserContext,
			final DefinitionBuilder definitionBuilder)
			throws LyciaParsingException {

		return transformElement(element, localName, parserContext,
				definitionBuilder);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sourceprojects.lycia.TagDefinitionParser#getTags()
	 */
	@Override
	public String[] getTags() {
		return new String[0];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.sourceprojects.lycia.TagDefinitionParser#getTagAnnotations()
	 */
	@Override
	public Tag[] getTagAnnotations() {
		if (getTags().length == 0) {
			return new Tag[0];
		}

		final List<Tag> tags = new ArrayList<Tag>();
		for (final String tag : getTags()) {
			tags.add(getTagAnnotationImpl(tag, getNamespace()));
		}

		return tags.toArray(new Tag[tags.size()]);
	}

	private Tag getTagAnnotationImpl(final String tag, final String namespace) {
		return new TagInnerImpl(tag, namespace, getNullXPathExpression(), false);
	}

	private XPath getNullXPathExpression() {
		return new XPathInnerImpl(XPath.NULL, getNamespaceArray());
	}

	private Namespace[] getNamespaceArray() {
		return ZERO_LENGTH_NAMESPACE_ARRAY;
	}

}
